const Base = require('./base.js');

module.exports = class extends Base {
  async indexAction() {
    const fields = [
      'id',
      'name',
      'primary_pic_url',
      'retail_price',
      'promotion_price',
      'group_price'
    ];
    const banner = await this.model('ad')
      // .where({ ad_position_id: 1 })
      .select();
    const channel = await this.model('channel')
      .order({ sort_order: 'asc' })
      .select();
    const newGoods = await this.model('goods')
      .field(fields)
      .where({ is_new: 1, is_on_sale: 1 })
      .order({ sort_order: 'desc' })
      .limit(4)
      .select();
    const hotGoods = await this.model('goods')
      .field(fields.push('goods_brief'))
      .order({ sort_order: 'desc' })
      .where({ is_hot: 1, is_on_sale: 1 })
      .limit(4)
      .select();
    const brandList = await this.model('brand')
      .where({ is_new: 1 })
      .order({ new_sort_order: 'desc' })
      .limit(4)
      .select();
    const fashionList = await this.model('goods')
      .field(fields.push('goods_brief'))
      .order({ sort_order: 'desc' })
      .where({ is_hot: 1, category_id: 5, is_on_sale: 1 })
      .limit(4)
      .select();
    const houseList = await this.model('goods')
      .field(fields.push('goods_brief'))
      .order({ sort_order: 'desc' })
      .where({ is_hot: 1, category_id: 1, is_on_sale: 1 })
      .limit(4)
      .select();
    const beautyList = await this.model('goods')
      .field(fields.push('goods_brief'))
      .order({ sort_order: 'desc' })
      .where({ is_hot: 1, category_id: 3, is_on_sale: 1 })
      .limit(4)
      .select();
    const groupList=await this.model('goods')
    .field(fields.push('goods_brief'))
    .order({ sort_order: 'desc' })
    .where({ is_group: 1,  is_on_sale: 1 })
    .limit(4)
    .select();
    // const topicList = await this.model('topic')
    //   .limit(4)
    //   .select();

    // const categoryList = await this.model('category')
    //   .where({ parent_id: 0, name: ['<>', '推荐'] })
    //   .select();
    // const newCategoryList = [];
    // for (const categoryItem of categoryList) {
    //   const childCategoryIds = await this.model('category')
    //     .where({ parent_id: categoryItem.id })
    //     .getField('id', 100);
    //   const categoryGoods = await this.model('goods')
    //     .field(['id', 'name', 'list_pic_url', 'retail_price'])
    //     .where({ category_id: ['IN', childCategoryIds] })
    //     .limit(7)
    //     .select();
    //   newCategoryList.push({
    //     id: categoryItem.id,
    //     name: categoryItem.name,
    //     goodsList: categoryGoods
    //   });
    // }

    return this.success({
      banner: banner,
      channel: channel,
      newGoodsList: newGoods,
      hotGoodsList: hotGoods,
      brandList: brandList,
      // topicList: topicList,
      // categoryList: newCategoryList,
      houseList,
      fashionList,
      beautyList,
      groupList
    });
  }
};
